import {Config} from 'config';

class Token {
    constructor(){
        this.verifyUrl = Config.restUrl + 'token/verify';
        this.tokenUrl = Config.restUrl = 'token/user';
    }
    verify(){
       wx.getStorageSync('token');
       if(!token){
         this.getTokenFromServer()
       }else {

       }
    }
    _veriryFromServer(token){
        var that = this;
        wx.request({
            url: that.verifyUrl,
            method: 'POST',
            data: {
                token:token
            },
            success:function (res) {
                var valid = res.data.isValid;
                if (valid){
                    that.getTokenFromServer();
                }
            }
        })
    }
    getTokenFromServer(cb){
      var that = this;
      wx.login({
        success:(res=>{
          wx.request({
            url: that.tokenUrl,
            method:"POST",
            data:{
              code:res.code
            },
            success:(res=>{
              wx.setStorageSync('token', res.data.token);
              cb && cb(res.data.token);
            })
          })
        })
      })
    }
}
export {Token}